package com.weizhi.logscripts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadFromFile {

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static String readFileByLines(String fileName,String empifileName) {
    	String resultfilename = fileName+"_error.txt";
    	resultfilename = resultfilename.replace("F:\\", "E:\\");
    	File resultfile = new File(resultfilename);
    	
    	boolean bool = false;
    	String result = "";
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
        	if(resultfile.exists()){
        		resultfile.delete();
        	}
        	resultfile.createNewFile();
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
            	//单独处理empi
            	empi(tempString, empifileName);
            	//单独处理verifyProcess
            	specialCheck(tempString);
            	specialCheck2(tempString);
                if(tempString.startsWith("INFO [Consum")){
                	bool = true;
                }
                if(bool&&tempString.startsWith(" ")){//以INFO开头，并且当前行是[TIME]开头，说明一次请求结束
                	bool = false;
                }
                result+=tempString+"\n";
                if(!bool){
                	String temp = result.replaceAll(" ", "").replaceAll("\n", "");
                	if(temp.length()>0){
                    	if(result.indexOf("[ERROR]")>0||result.indexOf("[WARN]")>0){
//                    		System.out.println(result);
                    		otherCheck(result,resultfilename);
//                        	System.out.println("-------------------------------");
                    	}
                	}
                	result="";
                	
                }
                
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return result ;
    }
    
    
    public static void otherCheck(String result,String resultfilename){
    	
    	if(result.indexOf("[WARN] \n[ice] connection exception:")>0){
    		return;
		}
    	if(result.indexOf("[ERROR] 调取tran_consumer平台接口错误消息没有找到或已经处理完毕")>0){
    		return;
    	}
    	if(result.indexOf("Caused by: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry")>0&&result.indexOf("for key 'PRIMARY'")>0){
    		return;
    	}
    	if(result.indexOf("[ERROR] 账单解锁true,账单ids：")>0){
    		return;
    	}
    	if(result.indexOf("\"detail\":\"状态已经被更新掉了，需要刷新重试\"")>0){
    		return;
    	}
//    	if(result.indexOf("\"detail\":\"状态已经被更新掉了，需要刷新重试\"")>0&&result.indexOf("[ERROR] 更新 verifyProcess状态 失败 ！")<0){
//    		return;
//    	}
    	if(result.indexOf("[ERROR] 支出后余额合计为负数，不能进行支出操作！")>0){
    		return;
    	}
    	if(result.indexOf("[WARN] 该账单下无人员信息：")>0&&result.indexOf("不做发单操作!")>0){
    		return;
    	}
    	if(result.indexOf("[ERROR] 雇员停保成功通知人才库失败：")>0){
    		return;
    	}
    	if(result.indexOf("[ERROR] 创建雇员通知人才库失败：")>0){
    		return;
    	}
    	if(result.indexOf("\"method\":\"weizhi.enterprise.declaration.reduceF\"")>0){
    		return;
    	}
    	if(result.indexOf("\"detail\":\"该订单上的信息不能被修改\"")>0){
    		return;
    	}
    	if(result.indexOf("ms.service.exception.DBException: ms.service.exception.DBException: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction")>0){
    		return;
    	}
    	if(result.indexOf("ms.service.exception.DBException: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction")>0){
    		return;
    	}
    	if(result.indexOf("Caused by: ms.service.exception.DBException: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction")>0){
    		return;
    	}
    	if(result.indexOf("[WARN] \n[ice] thread pool `Ice.ThreadPool.Server` is running low on threads")>0){
    		return;
    	}
    	if(result.indexOf("[ERROR] 更新审核状态出错！")>0&&result.indexOf("\"detail\":\"该雇员已经审核过，请刷新重试\"")>0){
    		return;
    	}
    	/***************************待定的过滤*********************************************/

//    	if(result.indexOf("weizhi.microseer.verifyProcess.getVerifyStatus")>0&&result.indexOf("陆施佩")>0&&result.indexOf("companyId=287829&method=weizhi.enterprise.declaration.changeState")>0){
//    		return;
//    	}

    	
    	
    	/***************************需要处理的方法*********************************************/
//    	if(result.indexOf("weizhi.enterprise.declaration.create")>0&&result.indexOf("\"companyId\":\"290981\"")>0&&result.indexOf("\"costCenterId\":\"2566\"")>0){
//    		return;
//    	}
//    	if(result.indexOf("weizhi.enterprise.declaration.changeState")>0&&result.indexOf("\"companyId\":\"290981\"")>0&&result.indexOf("\"operationType\":\"1\"")>0&&result.indexOf("\"state\":\"2\"")>0&&result.indexOf("\"serviceType\":\"1\"")>0){
//    		return;
//    	}
//    	if(result.indexOf("weizhi.enterprise.declaration.changeState")>0&&result.indexOf("companyId=290981")>0&&result.indexOf("operationType=1")>0&&result.indexOf("state=2")>0&&result.indexOf("serviceType=1")>0){
//    		return;
//    	}
//    	if(result.indexOf("weizhi.microseer.sys.createAppUser")>0&&result.indexOf("\\\"wid\\\":\\\"244754\\\"")>0){
//    		return;
//    	}

    	result =result+"-------------------------------\n";
		AppendToFile.appendMethodB(resultfilename, result);
    }
    
    /**
     * @Title: empi 
     * @author 林彬
     * @Description: empi警告处理 
     * @date 2017年1月19日 下午6:22:49 
     * @return void   
     * @throws
     */
    public static void empi(String result,String empifilename){
    	if(result.startsWith("[WARN] empId:")){
    		System.out.println(empifilename);
    		AppendToFile.appendMethodB(empifilename, result+"\n");
		}
    }
    
    public static void specialCheck(String result){
    	if(result.startsWith("[ERROR] 更新 verifyProcess状态 失败 ！")){
    		System.out.println(result);
    		ReadFileAndPath.set.add(result);
		}
    }
    
//    public static void specialCheck(String result){
//    	if(result.indexOf("RP170205234803437")>0&&result.indexOf("RP170205234811715")>0){
//    		System.out.println(result);
//    		ReadFileAndPath.set.add(result);
//		}
//    }
    
    public static void specialCheck2(String result){
    	if(result.startsWith("[WARN] 未找到雇员")){
    		System.out.println(result);
    		ReadFileAndPath.confindeeset.add(result);
		}
    }
    
    public static void verifyProcessState(){
    	String name = "F:\\verifyProcess.txt";
    	for(String result : ReadFileAndPath.set){
    		AppendToFile.appendMethodB(name, result+"\n");
    	}
    }
    
    public static void confindeeset(){
    	String name = "F:\\confindeeset.txt";
    	for(String result : ReadFileAndPath.confindeeset){
    		AppendToFile.appendMethodB(name, result+"\n");
    	}
    }

    
}
